package com.leo.action;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.RequestMap;

import com.leo.business.InterfaceBusiness;
import com.leo.entity.Score;
import com.leo.entity.Student;
import com.leo.formbean.InterfaceForm;
import com.leo.util.InnerObjectUtile;
import com.leo.util.JsonUtil;
import com.leo.vo.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class InterfaceAction extends ActionSupport implements ModelDriven<InterfaceForm> {
	@Resource
	InterfaceBusiness interfaceBusiness;
	private InterfaceForm interfaceForm = new InterfaceForm();
	
	public void getStuInfo(){
		Result result = interfaceBusiness.getStuInfo(interfaceForm);
		if(result.getResult().equals("success")){
			//过滤学生信息
			Student student = (Student) result.getData();
			Map<String,String> map = new HashMap<String,String>();
			map.put("className", student.getClassName());
			map.put("college", student.getCollege());
			map.put("entranceDate", student.getEntranceDate());
			map.put("grade", student.getGrade());
			map.put("name", student.getName());
			map.put("phone", student.getPhone());
			map.put("professional", student.getProfessional());
			map.put("sex", student.getSex());
			result.setData(map);
		}
		JsonUtil.output(result);
	}
	
	public void getScores(){
		Result result = interfaceBusiness.getScores(interfaceForm);
		if(result.getResult().equals("success")){
			//过滤学生信息
			List<Score> scores = (List<Score>) result.getData();
			List<Object > voScores = new LinkedList<Object>();
			for(Score score : scores){
				Map<String,String> map = new HashMap<String,String>();
				map.put("score", score.getScore());
				map.put("courName", score.getCourName());
				map.put("courID()", score.getCourID());
				map.put("courType", score.getCourType());
				map.put("credit", score.getCredit());
				map.put("gradePoint", score.getGradePoint());
				map.put("makeupScore", score.getMakeupScore());
				map.put("repairScore", score.getRepairScore());
				map.put("schoolYear", score.getSchoolYear());
				map.put("term", score.getTerm());
				voScores.add(map);
			}
			result.setData(voScores);
		}
		JsonUtil.output(result);
	}
	
	public String requestLogin(){
		String callback = ServletActionContext.getRequest().getHeader("Referer");
		if( !interfaceBusiness.validateCallback(interfaceForm,callback) ){
			InnerObjectUtile.REQUEST.getMap().put("cause","accessToken error");
			return ERROR;
		}
		//interfaceBusiness.validateCallback(interfaceForm);
		try{
			RequestMap request = (RequestMap)ActionContext.getContext().get("request");
			request.put("callback", callback);
			return SUCCESS;
			//AbstractMap
		}catch (Exception e) {
			InnerObjectUtile.REQUEST.getMap().put("cause","unknow error");
			return ERROR;
		}
	}
	
	//学生通过本系统登录之后，将安全的openID返回到callback地址，以参数的形式给开发者
	public void stuLogin(){
		if(this.interfaceForm.getUsername()!=null && this.interfaceForm.getPassword()!=null){
			//String callback = ServletActionContext.getRequest().getHeader("Referer");
			Result result = interfaceBusiness.stuLogin(interfaceForm);
			if(!result.getResult().equals("success")){
				JsonUtil.output(result);
				return;
			}
			Map<String,Object> m = new HashMap();
			m.put("result","success");
			m.put("callback",interfaceForm.getCallback());
			m.put("uid", result.getData());
			Result rt = Result.success();
			rt.setData(m);
			JsonUtil.output(rt);
			return;
		}
		JsonUtil.output(Result.passwordError());
	}

	@Override
	public InterfaceForm getModel() {
		return interfaceForm;
	}
}